package com.system.service;

import cn.hutool.core.util.StrUtil;
import com.rabbitmq.client.Channel;
import com.system.domain.Printingparameters;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson2.JSON;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static com.system.service.PrinterServiceImpl.executeCommand;

@Component
public class RabbitMqListener {

    @Autowired
    private PrinterService iprinterService;

    private final String serialNumber;

    // 通过构造函数注入 serialNumber
    public RabbitMqListener(String serialNumber) {
        this.serialNumber = serialNumber;
    }

    // 使用与RabbitMQConfig中相同的队列命名逻辑
    @RabbitListener(queues = "#{@serialNumber + '_QUEUE'}")
    public void Printer_QUEUE_Test(Object msg, Message message, Channel channel) throws Exception {
        String body = new String(message.getBody());
        Printingparameters printingparameter = JSON.parseObject(body, Printingparameters.class);

        if (StringUtils.isBlank(printingparameter.getPrinterSerivceName()) ||
                !Objects.equals(printingparameter.getPrinterSerivceName(), serialNumber)) {
            return;
        }
        String prefix = "Printer.";
        String SN ="";
        if (serialNumber.startsWith(prefix)) {
            SN = serialNumber.substring(prefix.length());
        }
        Boolean infoBySN = iprinterService.getInfoBySN(SN);
        if (!infoBySN) {
            System.out.println(serialNumber+"系统未授权 ！！！！！！！" );
            return;
        }
        String printerName = printingparameter.getPrinterName();

        System.out.println("服务器：" + serialNumber + "  打印机："+printerName+":开始打印...............");

        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("device_specs", printingparameter.getDeviceDetailAttachment());
        returnMap.put("device_fault", printingparameter.getDeviceFault());
        returnMap.put("device_condition", printingparameter.getDeviceCondition());
        returnMap.put("detype", printingparameter.getDetype());
        returnMap.put("DeviceModelId", printingparameter.getDeviceModelId());
        returnMap.put("remark", printingparameter.getRemark());

        iprinterService.generationContract("files/device_detail_ex_snap_biaoqian2.pdf", "./设备信息.pdf", returnMap, printingparameter);
        iprinterService.printPdf("./设备信息.pdf", printerName);
//        if( !(StringUtils.isBlank(printingparameter.getDeviceDetailAttachment()) && StringUtils.isBlank(printingparameter.getDeviceFault()))
//        ){
//            iprinterService.generationContract02("files/demo02.pdf", "./配件和故障信息.pdf", returnMap, printingparameter);
//            iprinterService.printPdf("./配件和故障信息.pdf", printerName);
//        }


    }
}